using System.Diagnostics;
using System.Xml;

namespace Vibstudio.X4NET.Xml.Comparision
{
    internal class Diffgram : DiffgramParentOperation
    {
        private XmlDiff _xmlDiff;
        private OperationDescriptor _descriptors;

        internal Diffgram(XmlDiff xmlDiff)
            : base(0)
        {
            _xmlDiff = xmlDiff;
        }

        internal void AddDescriptor(OperationDescriptor desc)
        {
            desc._nextDescriptor = _descriptors;
            _descriptors = desc;
        }

        internal override void WriteTo(XmlWriter xmlWriter, XmlDiff xmlDiff)
        {
            _xmlDiff = xmlDiff;
            WriteTo(xmlWriter);
        }

        internal void WriteTo(XmlWriter xmlWriter)
        {
            Debug.Assert(_xmlDiff._fragments != TriStateBool.DontKnown);

            xmlWriter.WriteStartDocument();

            xmlWriter.WriteStartElement(XmlDiff.Prefix, "xmldiff", XmlDiff.NamespaceUri);
            xmlWriter.WriteAttributeString("version", "1.0");
            xmlWriter.WriteAttributeString("sourceDocHash", _xmlDiff._sourceDoc.HashValue.ToString());
            xmlWriter.WriteAttributeString("options", _xmlDiff.GetXmlDiffOptionsString());
            xmlWriter.WriteAttributeString("fragments", (_xmlDiff._fragments == TriStateBool.Yes) ? "yes" : "no");

            WriteChildrenTo(xmlWriter, _xmlDiff);

            OperationDescriptor curOD = _descriptors;
            while (curOD != null)
            {
                curOD.WriteTo(xmlWriter);
                curOD = curOD._nextDescriptor;
            }

            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();
        }
    }
}